package com.zxx.autohomenavi.ziru_svg;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGAImageView;
import com.zxx.autohomenavi.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * @author zxx on 2021/8/23
 */
public class ZiroomTabView extends LinearLayout {

    private static String TAG = ZiroomTabView.class.getSimpleName();
    private List<String> titleList = new ArrayList<String>();

    private int selectedPosition = -1;
    private List<SVGAImageView> svgaImageViewList = new ArrayList<>();
    private List<ImageView> imageViewList = new ArrayList<>();
    private List<TextView> textViewList = new ArrayList<>();
    private ZiroomTabSelectedListener onTabSelectedListener;

    public ZiroomTabView(Context context) {
        this(context, null);
    }

    public ZiroomTabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZiroomTabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_tab_ziroom, this);
        titleList.add(getResources().getString(R.string.ziroom_tab_home));
        titleList.add(getResources().getString(R.string.ziroom_tab_search));
        titleList.add(getResources().getString(R.string.ziroom_tab_service));
        titleList.add(getResources().getString(R.string.ziroom_tab_lvju));
        titleList.add(getResources().getString(R.string.ziroom_tab_my));
        Log.e(TAG, "titleList大小：" + titleList.size());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int index = 0; index < getChildCount(); index++) {
            RelativeLayout child = (RelativeLayout) getChildAt(index);

            for (int i = 0; i < child.getChildCount(); i++) {
                View subChild = child.getChildAt(i);
                if (subChild instanceof SVGAImageView) {
                    svgaImageViewList.add((SVGAImageView) subChild);

                } else if (subChild instanceof ImageView) {
                    imageViewList.add((ImageView) subChild);

                } else if (subChild instanceof TextView) {
                    ((TextView) subChild).setText(titleList.get(index));
                    textViewList.add((TextView) subChild);
                }
            }

            final int finalIndex = index;
            child.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelectedPosition(finalIndex, true);
                }
            });
        }

        if (svgaImageViewList.size() > 0) {
            Log.e(TAG, "-->" + imageViewList.get(0).isSelected());
            svgaImageViewList.get(0).setVisibility(VISIBLE);
            svgaImageViewList.get(0).setCallback(null);
            svgaImageViewList.get(0).startAnimation();
            svgaImageViewList.get(0).stepToPercentage(0.1, true);
        }
    }

    /**
     * @param position       选中位置
     * @param invokeListener 是否需要触发OnTabSelectedListener
     */
    public void setSelectedPosition(int position, boolean invokeListener) {
        Log.e(TAG, "selectPosition:" + selectedPosition + " - position:" + position + " - " + invokeListener);
        //Log.e(TAG, "svgaImageViewList大小：" + svgaImageViewList.size());

        final ImageView img;
        SVGAImageView svga;
        if (selectedPosition >= 0 && selectedPosition != position) {
            textViewList.get(selectedPosition).setSelected(false);
            imageViewList.get(selectedPosition).setVisibility(VISIBLE);
            imageViewList.get(selectedPosition).setSelected(false);
            svgaImageViewList.get(selectedPosition).setCallback(null);
            svgaImageViewList.get(selectedPosition).stopAnimation();
            svgaImageViewList.get(selectedPosition).setVisibility(GONE);

            if (invokeListener && onTabSelectedListener != null) {
                onTabSelectedListener.onTabUnselected(selectedPosition);
            }
        }
        img = imageViewList.get(position);
        svga = svgaImageViewList.get(position);

        //Log.e(TAG, "svga：" + svga);

        textViewList.get(position).setSelected(true);
        img.setVisibility(INVISIBLE);

        //svga.startAnimation();
        svgaImageViewList.get(position).setCallback(new SVGACallback() {
            @Override
            public void onPause() {
                Log.e(TAG, "SVGACallback onPause() called");
            }

            @Override
            public void onFinished() {
                Log.e(TAG, "SVGACallback onFinished() called");
                img.setVisibility(VISIBLE);
                img.setSelected(true);
            }

            @Override
            public void onRepeat() {
                Log.e(TAG, "SVGACallback onRepeat() called");
            }

            @Override
            public void onStep(int i, double v) {
                //Log.e(TAG, "SVGACallback onStep() called");
            }
        });
        svgaImageViewList.get(position).stopAnimation();
        svgaImageViewList.get(position).stepToPercentage(0.1, true);
        svgaImageViewList.get(position).setVisibility(VISIBLE);

        if (selectedPosition == position) {
            if (invokeListener && onTabSelectedListener != null) {
                onTabSelectedListener.onTabReselected(selectedPosition);
            }
        } else {
            selectedPosition = position;
            if (invokeListener && onTabSelectedListener != null) {
                onTabSelectedListener.onTabSelected(selectedPosition);
            }
        }
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public ImageView getImageViewByPosition(int position) {
        if (position >= 0 && position <= imageViewList.size()) {
            return imageViewList.get(position);
        }
        return null;
    }

    public TextView getTextViewByPosition(int position) {
        if (position >= 0 && position <= textViewList.size()) {
            return textViewList.get(position);
        }
        return null;
    }

    public SVGAImageView getSVGAImageViewByPosition(int position) {
        if (position >= 0 && position <= svgaImageViewList.size()) {
            return svgaImageViewList.get(position);
        }
        return null;
    }

    public void setOnTabSelectedListener(ZiroomTabSelectedListener listener) {
        this.onTabSelectedListener = listener;
    }

    public interface ZiroomTabSelectedListener {
        void onTabSelected(int position);

        void onTabUnselected(int position);

        void onTabReselected(int position);
    }
}
